# tests/test_parser_with_file.py
import sys
sys.path.append("..")
sys.path.append(r"D:\Code\Crawler\weibo-crawler")
from core.parser import WeiboParser
import os

# 设置测试文件路径
TEST_USER_HTML_PATH = "../html/user_5787197886.html"
# TEST_USER_HTML_PATH = "../html/user_1644948230.html"
TEST_WEIBO_HTML_PATH = "../html/6819693315_page.html"

def read_from_file(path):
    if not os.path.exists(path):
        print(f"文件不存在: {path}")
        return

    with open(path, 'r', encoding='utf-8') as f:
        html = f.read()

    return html

def parse_user_info_from_file(path):
    html = read_from_file(path)
    # 测试用户信息解析
    user_info = WeiboParser.parse_user_info(html)
    print("解析出的用户信息:")
    print(user_info)

def parse_weibo_list_from_file(path):
    html = read_from_file(path)
    # 测试微博内容解析
    weibo_list = WeiboParser.parse_weibo_list(html)
    print("\n解析出的微博列表（前3条）:")
    for w in weibo_list[:3]:
        print(w)


if __name__ == '__main__':
    parse_user_info_from_file(TEST_USER_HTML_PATH)
    # parse_weibo_list_from_file(TEST_WEIBO_HTML_PATH)
